import {Trigger} from "solar/w3ts/handles/trigger";
import SelectUtil from "solar/util/SelectUtil";
import TextTagUtil from "solar/util/TextTagUtil";


export default class AddResourceByUnitTypeOnTimerState {
    static config: {
        [id: string]: {
            addGold?: number
            addLumber?: number
        }
    } = {}


    constructor() {
        let trigger = new Trigger()
        trigger.registerTimerEvent(1, true);
        trigger.addAction(this.action)

    }

    action(this: void) {
        SelectUtil.forAllUnits(unit => {
            let cfgData = AddResourceByUnitTypeOnTimerState.config[id2string(GetUnitTypeId(unit))]
            if (cfgData) {
                let player = GetOwningPlayer(unit);
                if (cfgData.addGold && cfgData.addGold != 0) {
                    SetPlayerState(player, PLAYER_STATE_RESOURCE_GOLD, GetPlayerState(player, PLAYER_STATE_RESOURCE_GOLD) + cfgData.addGold);
                    TextTagUtil.text(cfgData.addGold + "", unit, 15, 0.5, 205, 127, 50)
                }
                if (cfgData.addLumber && cfgData.addLumber != 0) {
                    SetPlayerState(player, PLAYER_STATE_RESOURCE_LUMBER, GetPlayerState(player, PLAYER_STATE_RESOURCE_LUMBER) + cfgData.addLumber);
                    TextTagUtil.text(cfgData.addLumber + "", unit, 15, 0.5, 0, 255, 0)
                }
            }
        })
    }


}

